package cn.fxl.org.controller;

import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.org.domain.Employee;
import cn.fxl.org.query.EmployeeQuery;
import cn.fxl.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description = "员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;

    @GetMapping
    @ApiOperation("获取所有员工信息")
    public List<Employee> getAll() {
        return iEmployeeService.getAll();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取一个员工信息")
    public Employee getOne(@PathVariable("id") Long id) {
        return iEmployeeService.getOne(id);
    }

    @PostMapping
    @ApiOperation("高级查询分页查询")
    public GmResult<Employee> querPage(@RequestBody EmployeeQuery employeeQuery) {
        return iEmployeeService.Pagequery(employeeQuery);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除员工")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            iEmployeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, "删除失败");
        }
    }

    @PutMapping
    @ApiOperation("新增或修改有id为修改，没有id为新增")
    public AjaxResult saveAndUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                iEmployeeService.save(employee);
            } else {
                iEmployeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, "操作失败");
        }
    }
    @PatchMapping
    @ApiOperation("批量删除")
    public AjaxResult pathchdel(@RequestBody Long[] a){
        try {
            iEmployeeService.pathchdel(a);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"删除失败");
        }
    }

    @GetMapping("/list/{id}")
    @ApiOperation("根据shopid获取员工")
    public List<Employee> getEmpByShopId(@PathVariable("id") Long id) {

           return iEmployeeService.getEmpByShopId(id);


    }
}
